import copy
from scopt.util.message_types import STRING


class HandlerEnhancer(object):
    def __init__(self):
        self.__registry = {}

    def __call__(self, type, version, reqd, resd, note=''):
        def enhance(f):
            f.TYPE = type
            f.VERSION = version
            f.REQUEST = _request(type, reqd)
            f.RESPONSE = _response(resd, note)
            self.__registry.update({f.TYPE + '.' + f.VERSION: f})
            return f
        return enhance

    def registry(self):
        return self.__registry


def _request(msg_type, reqd):
    def f():
        return {'reqh': request_header_template(msg_type),
                'reqd': copy.deepcopy(reqd)}
    return f


def _response(resd, note):
    def f():
        return {'resh': response_header_template(note),
                'resd': copy.deepcopy(resd)}
    return f


def request_header_template(msg_type):
    return {'security': {},
            'language': '',
            'format': '',
            'message_id': '',
            'type': msg_type,
            'tenant_id': STRING(required=True),
            'version': STRING(required=True)}


def response_header_template(note):
    return {'message_id': STRING(required=True),
            'status': STRING(required=True, default='OK'),
            'note': note}
